﻿using Dapper;
using HR.Config;
using HR.IService;
using HR.Models;
using System.Data.SqlClient;

namespace HR.Service
{
    public class SalaryStandardService : ISalaryStandardService
    {
        private readonly HRDBContext db;
        private readonly IConfiguration icon;
        private readonly string? constr;

        public SalaryStandardService(HRDBContext db, IConfiguration icon)
        {
            this.db = db;
            this.icon = icon;
            this.constr = this.icon["ConnectionStrings"];
        }

        public Task<List<config_public_char>> SalaryConst()
        {
            return Task.Run(() =>
            {
                return db.config_public_chars.Where(cpc => cpc.attribute_kind == "薪酬设置").ToList();
            });
        }


        public Task<List<user>> FindUsers()
        {
            return Task.Run(() =>
            {
                List<user> users = db.users.ToList();
                return users;
            });
        }
        public Task<string> FindNewStandard_id()
        {
            return Task.Run(() =>
            {
                //查询最新的薪酬标准编号
                salary_standard? ss = db.salary_standards.OrderByDescending(ss => ss.ssd_id).FirstOrDefault();
                return NextNumber(ss.standard_id);
            });
        }
        // 自动生成下一个薪酬标准ID
        public string NextNumber(string BaseNumber)
        {
            string NewNumber = "";//新值
            int InNumber = 1;//进位
            int PlaceValue;//位值
            char[] No = BaseNumber.ToCharArray();

            for (int i = BaseNumber.Length - 1; i >= 0; i--)
            {
                if (No[i] == '9' && InNumber == 1)
                {
                    InNumber = 1;
                    NewNumber = "0" + NewNumber;
                }
                else
                 if (InNumber == 1 && No[i] >= '0' && No[i] < '9')
                {
                    PlaceValue = Int32.Parse(No[i].ToString());
                    PlaceValue = (InNumber + PlaceValue);
                    InNumber = 0;
                    NewNumber = PlaceValue.ToString() + NewNumber;
                }
                else
                {
                    InNumber = 0;
                    NewNumber = No[i] + NewNumber;
                }
            }
            if (BaseNumber == NewNumber)
                NewNumber = "0000000001";
            return NewNumber;
        }

        public Task<Paging<salary_standard>> SalaryFillIsNoCheck(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                List<salary_standard> list = db.salary_standards.Where(ss => ss.check_status == 0).ToList();
                List<salary_standard> tabs = list.Skip((pgcurrent - 1) * pgsize).Take(pgsize).ToList();
                Paging<salary_standard> pg = new Paging<salary_standard>()
                {
                    rows = list.Count,
                    tabs = tabs
                };
                return pg;
            });
        }

        public Task<int> SalaryStandardAdd(salary_standard ss)
        {
            return Task.Run(() =>
            {
                db.salary_standards.Add(ss);
                return db.SaveChanges();
            });
        }

        public Task<int> SalaryStandardCheck(salary_standard ss)
        {
            return Task.Run(() =>
            {
                salary_standard? ssd = db.salary_standards.Find(ss.ssd_id);
                ssd.checker = ss.checker;
                ssd.check_time = ss.check_time;
                ssd.check_status = ss.check_status;
                ssd.check_comment = ss.check_comment;
                return db.SaveChanges();
            });
        }

        public Task<Paging<salary_standard>> SalaryStandardDimFind(SalaryStandardDimPage ssdp)
        {
            return Task.Run(() =>
            {
                Paging<salary_standard> pg = new Paging<salary_standard>();
                using (SqlConnection sqlc = new SqlConnection(constr))
                {
                    string sql = $@"select * from salary_standard where check_status='{ssdp.uqFlag}' and standard_id like '%{ssdp.id}%' and 
                    (standard_name like '%{ssdp.key_word}%' or designer like '%{ssdp.key_word}%' or checker like '%{ssdp.key_word}%' or changer like '%{ssdp.key_word}%')";
                    if (ssdp.open_time != null || ssdp.close_time != null)
                    {
                        sql += @$" and regist_time <= '{ssdp.close_time}' and regist_time>= '{ssdp.open_time}'";
                    }
                    //拿到筛选后的所有集合
                    List<salary_standard> list = sqlc.Query<salary_standard>(sql).ToList();
                    //分页操作
                    List<salary_standard> tabs = list.Skip((ssdp.pgcurrent - 1) * ssdp.pgsize).Take(ssdp.pgsize).ToList();
                    pg.tabs = tabs;
                    pg.rows = list.Count;
                }
                return pg;
            });
        }

        public Task<int> SalaryStandardUdp(salary_standard ss)
        {
            return Task.Run(() =>
            {
                salary_standard? ssd = db.salary_standards.Find(ss.ssd_id);
                ssd.standard_name = ss.standard_name;
                ssd.designer = ss.designer;
                ssd.changer = ss.changer;
                ssd.change_time = ss.change_time;
                ssd.salary_sum = ss.salary_sum;
                ssd.change_status = ss.change_status;
                ssd.remark = ss.remark;
                return db.SaveChanges();
            });
        }
    }
}
